package com.ftg.youx.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.fastjson2.JSON;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ftg.youx.entity.SysEmp;
import com.ftg.youx.service.SysEmpService;
import com.ftg.youx.tools.R;
import com.ftg.youx.tools.S;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * apifox : 下载  在线 注册一个帐号 :
 *          学会使用apifox
 *
 * https://www.runoob.com/w3cnote/getting-started-with-typescript.html
 *
 * Harmoneyos
 * @author 布衣
 */
@RestController
@RefreshScope
@RequestMapping("/sys/emp")
@Slf4j
public class SysEmpController {

    @Autowired
    SysEmpService sysEmpService;
    //spring-el expression language
    @Value("${spring.dev.name}")
    String name;

    @GetMapping("/getName")
    public String getNameByNacos() {
        return "===>" + name + "我是一台服务器，8082";
    }

    /**
     * 分页查询
     *
     * @param page 页码
     * @param size 显示 条数
     * @return R
     */
    @GetMapping("/list")
//    @SentinelResource(value = "list",blockHandler = "returnString")
    public String list(
            @RequestParam("page") Integer page,
            @RequestParam("size") Integer size,
            @RequestParam(value = "name", required = false) String name
    ) {
        IPage<SysEmp> ipage = new Page<>(page, size);
        QueryWrapper<SysEmp> query = new QueryWrapper<>();
        if (name != null && !name.isEmpty()) {
            query.likeRight("name", name);
        }
        System.out.println("我是8082");
        return JSON.toJSONString(R.ok(sysEmpService.page(ipage, query)));
    }




    @PostMapping("/save")
    public R<Boolean> save(@RequestBody SysEmp sysEmp) {
        return R.ok(sysEmpService.save(sysEmp));
    }

    @PutMapping("/update")
    public R<Boolean> update(@RequestBody SysEmp sysEmp) {
        return R.ok(sysEmpService.updateById(sysEmp));
    }

    @DeleteMapping("/delById/{id}")
    public R<Boolean> delById(@PathVariable("id") Integer id) {
        return R.ok(sysEmpService.removeById(id));
    }

    /**
     * 为订单创建人员提供相应的方法与功能
     *
     * @return String
     */
    @GetMapping("/getOrderEmpId")
    public String getOrderEmpId() {
        return JSON.toJSONString(R.ok(sysEmpService.getOrderEmpId()));
    }
}
